import * as PIXI from 'pixi.js'
import useCardsStore from '@/store/modules/useCardsStroe'
import { localeHttp } from '@/api'
import { nameSeries, nameCategory, nameElement, nameType, nameCard, nameSkillConsume, rankNames } from '@/config'
const cardsStore = useCardsStore()
const data = await localeHttp.Get<Wx.Card.Data[]>('/json/cards.json')
cardsStore.setCards(data)
const cardsPath: { [key: string]: string } = {}
data.forEach(card => {
  cardsPath[card.id] = `/assets/sprite/card/${nameCard(card)}`
  PIXI.Assets.add({ alias: 'card_' + card.id, src: `/assets/sprite/card/${nameCard(card)}` })
})

const diyData = await localeHttp.Get<Wx.Card.Data[]>('/json/diys.json')
cardsStore.setDiys(diyData)
diyData.forEach(diy => {
  cardsPath[diy.id] = `/assets/sprite/card/${nameCard(diy)}`
  PIXI.Assets.add({ alias: 'card_' + diy.id, src: `/assets/sprite/card/${nameCard(diy)}` })
})
// PIXI.Assets.addBundle('cards', cardsPath)
// 卡册
PIXI.Assets.addBundle('cardBook', {
  cardBook: '/assets/cardBook.png',
  HallBG_Back: '/assets/HallBG_Back.png',
  logo_h: '/assets/logo_h.png',
  bxlogo: '/assets/bxlogo.png',
  yellow: '/assets/yellow.png',
})
PIXI.Assets.addBundle('module', {
  btns: '/assets/sprite/btns.png',
  close: '/assets/close.png',
  diyBg: '/assets/diyBg.png',
})
PIXI.Assets.addBundle('cardModule', {
  category: '/assets/sprite/category.png',
  element: '/assets/sprite/element.png',
  series: '/assets/sprite/series.png',
  type: '/assets/sprite/type.png',
  skillConsume: '/assets/sprite/skillConsume.png',
  sprite: '/assets/sprite/sprite.png',
})

// 卡牌模块数据
const moduleData: Record<string, Wx.Sprite.Data | null> = {
  category: null,
  element: null,
  series: null,
  type: null,
  skillConsume: null,
  btns: null,
  sprite: null,
}
const modulePromise: Record<string, Promise<{ frames: Wx.Sprite.Data }> | null> = {
  category: null,
  element: null,
  series: null,
  type: null,
  skillConsume: null,
  btns: null,
  sprite: null,
}
const getModuleData = async (name: string) => {
  if (moduleData[name] === null) {
    if (modulePromise[name] === null) {
      modulePromise[name] = localeHttp.Get<{ frames: Wx.Sprite.Data }>(`/assets/sprite/${name}.json`)
    }
    // 载入数据
    const { frames } = await modulePromise[name]
    Object.entries(frames).forEach(([, value]) => {
      const { x, y, w, h } = value.frame
      value._frame = [x, y, w, h]
    })
    moduleData[name] = frames
  }
  return moduleData[name]
}
/** 卡牌类型*/
export const typeModule = async (cardData: Wx.Card.Param) => {
  const bundle = await PIXI.Assets.loadBundle('cardModule')
  const { _frame } = (await getModuleData('type'))[nameType(cardData)]
  return new PIXI.Sprite(
    new PIXI.Texture({
      source: bundle.type,
      frame: new PIXI.Rectangle(..._frame!),
    }),
  )
}
/** 卡牌系列和品阶*/
export const seriesModule = async (cardData: Wx.Card.Param) => {
  const bundle = await PIXI.Assets.loadBundle('cardModule')
  const { _frame } = (await getModuleData('series'))[nameSeries(cardData)]
  return new PIXI.Sprite(
    new PIXI.Texture({
      source: bundle.series,
      frame: new PIXI.Rectangle(..._frame!),
    }),
  )
}
/** 卡牌类别 */
export const categoryModule = async (cardData: Wx.Card.Param) => {
  const bundle = await PIXI.Assets.loadBundle('cardModule')
  const { _frame } = (await getModuleData('category'))[nameCategory(cardData)]
  return new PIXI.Sprite(
    new PIXI.Texture({
      source: bundle.category,
      frame: new PIXI.Rectangle(..._frame!),
    }),
  )
}
/** 卡牌五行*/
export const elementModule = async (cardData: Wx.Card.Param, index: number) => {
  const bundle = await PIXI.Assets.loadBundle('cardModule')
  const { _frame } = (await getModuleData('element'))[nameElement(cardData, index)]
  return new PIXI.Sprite(
    new PIXI.Texture({
      source: bundle.element,
      frame: new PIXI.Rectangle(..._frame!),
    }),
  )
}
/** 卡牌技能消耗*/
export const skillConsumeModule = async (cardData: Wx.Card.Param) => {
  const bundle = await PIXI.Assets.loadBundle('cardModule')
  const { _frame } = (await getModuleData('skillConsume'))[nameSkillConsume(cardData)]
  return new PIXI.Sprite(
    new PIXI.Texture({
      source: bundle.skillConsume,
      frame: new PIXI.Rectangle(..._frame!),
    }),
  )
}
/** 按钮精灵*/
export const btnsModule = async (name: string) => {
  const bundle = await PIXI.Assets.loadBundle('module')
  const { _frame } = (await getModuleData('btns'))[name]
  return new PIXI.Sprite(
    new PIXI.Texture({
      source: bundle.btns,
      frame: new PIXI.Rectangle(..._frame!),
    }),
  )
}

export const rankSprites = async () => {
  const bundle = await PIXI.Assets.loadBundle('cardModule')
  const names = rankNames()
  return await Promise.all(
    Object.values(names).map(async name => {
      const { _frame } = (await getModuleData('sprite'))[name]
      return new PIXI.Sprite(
        new PIXI.Texture({
          source: bundle.sprite,
          frame: new PIXI.Rectangle(..._frame!),
        }),
      )
    }),
  )
}

export const createSprite = async (name: string) => {
  const bundle = await PIXI.Assets.loadBundle('cardModule')
  const { _frame } = (await getModuleData('sprite'))[name]
  return new PIXI.Sprite(
    new PIXI.Texture({
      source: bundle.sprite,
      frame: new PIXI.Rectangle(..._frame!),
    }),
  )
}

export const createNsSprite = async (name: string, width?: number, height?: number) => {
  const bundle = await PIXI.Assets.loadBundle('cardModule')
  const { _frame, borders } = (await getModuleData('sprite'))[name]
  return new PIXI.NineSliceSprite({
    texture: new PIXI.Texture({
      source: bundle.sprite,
      frame: new PIXI.Rectangle(..._frame!),
    }),
    leftWidth: borders.left,
    rightWidth: borders.right,
    topHeight: borders.top,
    bottomHeight: borders.bottom,
    width,
    height,
  })
}
